package com.example.expression;

import com.example.security.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component("ex")
public class CustomExpressionRoot {

    public boolean hasAnyAuthority(String... authority){
        // 获取当前用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        List<String> permissions = loginUser.getPermission();
        // 判断用户权限集合中是否存在authority
        List<String> list = Arrays.stream(authority).filter(permissions::contains)
                .collect(Collectors.toList());

        return list.size() > 0;
    }
}